'''基础层：封装元素定位和操作；'''
from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
import logging

logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s')


class BasePage:
    def __init__(self, driver):
        self.driver = driver

    def get_url(self, url):
        self.driver.get(url)

    def find_ele(self, loc):
        # 实现定位输入框username，password
        ele = self.driver.find_element(*loc)
        logging.info(f'定位{loc}pass')
        return ele

    def input_ele(self, loc, value):
        # 实现输入框输入数据
        ele = self.find_ele(loc)
        ele.send_keys(value)
        logging.info(f'在{loc}位置输入{value}成功')

    def ele_click(self, loc):
        # 实现点击按钮
        ele = self.find_ele(loc)
        ele.click()
        logging.info(f'点击{loc}成功')

    def ele_clear(self, loc):
        # 清空输入框
        ele = self.find_ele(loc)
        ele.clear()
        logging.info(f'在{loc}清空内容成功')


if __name__ == '__main__':
    url = r'http://192.168.232.129/upload'
    username_loc = (By.ID, 'ls_username')
    password_loc = (By.ID, 'ls_password')
    value1 = 'admin'
    value2 = '123456'
    login_loc = (By.XPATH, '//*[@id="lsform"]/div/div/table/tbody/tr[2]/td[3]/button/em')

    # 测试代码，没有引入option
    chrome_location = r'E:\python_pro\Win_x64_1135105_chrome-win\chrome-win\chrome.exe'
    options = webdriver.ChromeOptions()
    options.binary_location = chrome_location

    driver = webdriver.Chrome(options=options)

    discuz_page = BasePage(driver)
    discuz_page.get_url(url)
    discuz_page.ele_clear(username_loc)
    discuz_page.input_ele(username_loc, value1)
    discuz_page.ele_clear(password_loc)
    discuz_page.input_ele(password_loc, value2)
    sleep(3)
    discuz_page.ele_click(login_loc)
    sleep(3)

    driver.quit()
